﻿/* ============================================================================
(c) Copyright 2012 Hewlett-Packard Development Company, L.P.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge,publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
============================================================================ */
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HPCloud.Objects.DataAccess;
using HPCloud.Test;
using HPCloud.Objects.Domain.Compute.Security;
using System.Threading;

namespace HPCloud.Objects.Test.Unit_Test.Compute
{
    [TestClass]
    public class SecurityTest : BaseUnitTest
    {

        #region SecurityGroup Test
//=========================================================================================
/// <summary>
/// 
/// </summary>
//=========================================================================================
        [TestMethod]
        public void SecurityGroup_GetAll()
        {
            ISecurityRepository repository                     = this.Factory.CreateSecurityRepository();
            List<Domain.Compute.Security.SecurityGroup> groups = repository.GetSecurityGroups();

            Assert.IsTrue(groups.Count() > 0);
        }
//=========================================================================================
/// <summary>
/// 
/// </summary>
//=========================================================================================
        [TestMethod]
        public void SecurityGroup_GetDetails()
        {
            ISecurityRepository repository = this.Factory.CreateSecurityRepository();
            SecurityGroup group            = repository.GetSecurityGroup("222");

            Assert.IsNotNull(group);
        }
//=========================================================================================
/// <summary>
/// 
/// </summary>
//=========================================================================================
        [TestMethod]
        public void SecurityGroup_Delete()
        {
            SecurityGroup group            = this.CreateTestGroup();
            ISecurityRepository repository = this.Factory.CreateSecurityRepository();

            repository.DeleteSecurityGroup(group.Id);
            Thread.Sleep(15000);
            SecurityGroup testgroup = repository.GetSecurityGroup(group.Id);
            Assert.IsNull(testgroup);
        }
//=========================================================================================
/// <summary>
/// 
/// </summary>
//=========================================================================================
        [TestMethod]
        public void SecurityGroup_Save()
        {
            ISecurityRepository repository                     = this.Factory.CreateSecurityRepository();
            List<Domain.Compute.Security.SecurityGroup> groups = repository.GetSecurityGroups();
            SecurityGroup secGroup                             = groups.Where(gn => gn.Name == "SomeTestGroup").Single();

            if (secGroup != null) {
                repository.DeleteSecurityGroup(secGroup.Id);
            }

            NewSecurityGroup group         = new NewSecurityGroup();
            group.Description              = "Simply another test";
            group.Name                     = "SomeTestGroup";

            SecurityGroup newGroup = repository.SaveSecurityGroup(group);
            Assert.IsNotNull(newGroup);
        }
        #endregion
        #region KeyPair Test
//=========================================================================================
/// <summary>
/// 
/// </summary>
//=========================================================================================
        [TestMethod]
        public void KeyPairs_GetAll()
        {
            ISecurityRepository repository = this.Factory.CreateSecurityRepository();
            List<KeyPair> kps              = repository.GetKeyPairs();
            Assert.IsTrue(kps.Count() > 0);
        }
//=========================================================================================
/// <summary>
/// 
/// </summary>
//=========================================================================================
        [TestMethod]
        public void KeyPairs_Save()
        {
            ISecurityRepository repository = this.Factory.CreateSecurityRepository();
            NewKeyPair nkp                 = new NewKeyPair();
            nkp.Name                       = Guid.NewGuid().ToString();
            KeyPair newKeyPair             = repository.SaveKeyPair(nkp);

            repository.DeleteKeyPair(nkp.Name);
            Assert.IsNotNull(newKeyPair);
        }
//=========================================================================================
/// <summary>
/// 
/// </summary>
//=========================================================================================
        [TestMethod]
        public void KeyPair_Delete()
        {
            ISecurityRepository repository = this.Factory.CreateSecurityRepository();
            NewKeyPair nkp                 = new NewKeyPair();
            nkp.Name                       = Guid.NewGuid().ToString();
            KeyPair newKeyPair             = repository.SaveKeyPair(nkp);

            repository.DeleteKeyPair(nkp.Name);

            List<KeyPair> kps = repository.GetKeyPairs();
            if (kps != null)
               Assert.IsFalse(kps.Where(k => k.Name == nkp.Name).Any());
        }
        #endregion
        #region SecurityGroupRule Test
//=========================================================================================
/// <summary>
/// 
/// </summary>
//=========================================================================================
        private SecurityGroup CreateTestGroup ()
        {
            ISecurityRepository repository = this.Factory.CreateSecurityRepository();
            NewSecurityGroup group         = new NewSecurityGroup();
            group.Description              = "Simply another test";
            group.Name                     = Guid.NewGuid().ToString();

            return repository.SaveSecurityGroup(group);
        }
//=========================================================================================
/// <summary>
/// 
/// </summary>
//=========================================================================================
        [TestMethod]
        public void SecurityGroupRule_Save()
        {
            ISecurityRepository repository = this.Factory.CreateSecurityRepository();
            //SecurityGroup group            = this.CreateTestGroup();
            NewSecurityRule rule           = new NewSecurityRule();

            rule.FromPort      = "3389";
            rule.ToPort        = "3389";
            rule.Name          = "rdpRuleTest";
            rule.IpProtocol    = "tcp";
            rule.ParentGroupId = "23575"; // group.Id;
            
            SecurityGroupRule newRule = repository.SaveSecurityRule(rule);
            //repository.DeleteSecurityRule(newRule.Id);
            //repository.DeleteSecurityGroup(group.Id);

            Assert.IsNotNull(newRule);
        }
//=========================================================================================
/// <summary>
// From the CLI use this -> add-rule -ipr "iprTest" -IP "tcp" -tp "84" -fp "81" -gid "5345"
/// </summary>
//=========================================================================================
        [TestMethod]
        public void SecurityGroupRule_Delete()
        {
            SecurityGroup group            = this.CreateTestGroup();
            ISecurityRepository repository = this.Factory.CreateSecurityRepository();
            NewSecurityRule rule           = new NewSecurityRule();

            rule.FromPort      = "80";
            rule.ToPort        = "81";
            rule.Name          = Guid.NewGuid().ToString();
            rule.IpProtocol    = "tcp";
            rule.ParentGroupId = group.Id;

            // Not sure how to do the assertion here as OpenStack doesnt have a GetSecurityRule method.

            SecurityGroupRule newRule = repository.SaveSecurityRule(rule);
            repository.DeleteSecurityRule(newRule.Id);
            repository.DeleteSecurityGroup(group.Id);
        }
        #endregion
    }
}
